package com.pn.service.impl;

import com.pn.entity.Result;
import com.pn.entity.User;
import com.pn.entity.UserRole;
import com.pn.entity.dto.AssignRoleDto;
import com.pn.mapper.RoleMapper;
import com.pn.mapper.UserMapper;
import com.pn.mapper.UserRoleMapper;
import com.pn.page.Page;
import com.pn.service.UserService;
import com.pn.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public User queryUserByCode(String userCode) {
        return userMapper.findUserByCode(userCode);
    }

    @Override
    public Page queryUserByPage(Page page, User user) {

        // 查询用户行数
        Integer rowCount = userMapper.findUserRowCount(user);
        // 分页查询用户
        List<User> userList = userMapper.findUserByPage(page, user);
        // 组装所有分页信息
        page.setTotalNum(rowCount);
        page.setResultList(userList);
        return page;
    }

    @Override
    public Result saveUser(User user) {

        //判断账号是否唯一
        User info = userMapper.findUserByCode(user.getUserCode());

        if (info != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "账号已存在！");
        }

        // 对密码做加密处理
        String password = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(password);

        // 执行添加
        int result = userMapper.insertUser(user);
        if (result > 0) {
            return Result.ok("用户添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户添加失败！");
    }

    @Override
    public Result setUserState(User user) {
        int userId = user.getUserId();
        String userState = user.getUserState();
        int updateBy = user.getUpdateBy();

        int result = userMapper.updateUserStateByUid(userId, userState, updateBy);
        if (result > 0) {
            return Result.ok("用户修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户修改失败！");
    }

    @Override
    public Result removeUserByIds(List<Integer> ids) {

        int result = userMapper.updateIsDeleteByUids(ids);
        if (result > 0) {
            return Result.ok("用户删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户删除失败！");
    }

    @Override
    public Result setUserByUid(User user) {

        int result = userMapper.updateUserByUid(user);
        if (result > 0) {
            return Result.ok("用户修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "用户修改失败！");
    }

    @Override
    public Result setPwdByUid(Integer userId) {

        // 给定初始密码 123456
        String password = DigestUtil.hmacSign("123456");

        int result = userMapper.updatePwdByUid(userId, password);
        if (result > 0) {
            return Result.ok("重置密码成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS, "重置密码失败！");
    }

    @Transactional
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {

        userRoleMapper.deleteUserRoleByUid(assignRoleDto.getUserId());

        List<String> roleCheckList = assignRoleDto.getRoleCheckList();
        for (String roleName : roleCheckList) {
            Integer roleId = roleMapper.findRoleIdByRName(roleName);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(assignRoleDto.getUserId());
            userRoleMapper.insertUserRoleByUid(userRole);
        }
    }
}
